﻿@model IEntityViewModel<Thread>
@{
    Forum forum = ViewBag.Forum;
    ViewBag.Title = "板块 - " + forum.Name;
    var threads = await forum.LoadAsync(t => t.Threads);
    var today = DateTime.Today;
    var todayCount = await threads.CountAsync(t => t.CreateDate >= today);
}
<ol class="breadcrumb">
    <li><a asp-controller="Home" asp-action="Index">论坛</a></li>
    <li class="active">@forum.Name</li>
</ol>
<div class="page-header">
    <h1>@forum.Name<small> 今日：@todayCount。</small></h1>
</div>
<div class="row">
    <div class="col-xs-6">
        @if (User.Identity.IsAuthenticated)
        {
            <div class="btn-group">
                <a href="@Url.Action("Create", new { id = forum.Index })" class="btn btn-danger">发帖</a>
                <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <span class="caret"></span>
                    <span class="sr-only">Toggle Dropdown</span>
                </button>
                <ul class="dropdown-menu">
                    <li><a href="#">Action</a></li>
                    <li><a href="#">Another action</a></li>
                    <li><a href="#">Something else here</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">Separated link</a></li>
                </ul>
            </div>
        }
    </div>
    <div class="col-xs-6 text-right">
        <div class="btn-group">
            @if (Model.CurrentPage > 3)
            {
                <button type="submit" name="page" value="1" class="btn btn-default" @(Model.CurrentPage == 1 ? " disabled=\"disabled\"" : "" )>1</button>
                if (Model.CurrentPage > 4)
                {
                    <button type="button" class="btn btn-default disabled">.</button>
                }
            }
            @for (int i = Model.CurrentPage - 2; i <= Model.CurrentPage + 2; i++)
            {
                if (i < 1)
                {
                    continue;
                }
                if (i > Model.TotalPage)
                {
                    break;
                }
                <button type="submit" name="page" value="@i" class="btn btn-default @(Model.CurrentPage == i ? "active" : "")">@i</button>
            }
            @if (Model.CurrentPage < Model.TotalPage - 2)
            {
                if (Model.CurrentPage < Model.TotalPage - 3)
                {
                    <button type="button" class="btn btn-default disabled">.</button>
                }
                <button type="submit" name="page" value="1" class="btn btn-default" @(Model.CurrentPage == Model.TotalPage ? " disabled=\"disabled\"" : "" )>6</button>
            }
        </div>
    </div>
</div>
<br />
<table class="table table-bordered table-hover">
    <thead>
        <tr style="background-color: lightgray;">
            <td style="width: 32px; text-align: center;"></td>
            <td>标题</td>
            <td style="width: 120px; text-align: center;">作者</td>
            <td style="width: 100px; text-align: center;">回复/查看</td>
            <td style="width: 120px; text-align: center;">最后发表</td>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Items)
        {
            var replies = await item.LoadAsync(t => t.Replies);
            <tr>
                <td><a target="_blank" href="@Html.Raw(Url.Action("Index", "Thread", new { id = item.Index }))"><span class="glyphicon glyphicon-new-window" aria-hidden="true"></span></a></td>
                <td><a href="@Html.Raw(Url.Action("Index", "Thread", new { id = item.Index }))">@item.Title</a></td>
                <td style="text-align: center; vertical-align: middle; padding: 0; line-height: 16px;">
                    <small>@item.Member.Username</small>
                    <br />
                    <small>@item.CreateDate.ToString("yyyy-MM-dd")</small>
                </td>
                <td style="text-align: center; vertical-align: middle; padding: 0; line-height: 16px;">
                    <small>@replies.Count</small>
                    <br />
                    <small>0</small>
                </td>
                <td style="text-align: center; vertical-align: middle; padding: 0; line-height: 16px;">
                    @{
                        var last = await replies.Include(t => t.Member).OrderByDescending(t => t.CreateDate).FirstAsync();
                        <small>@last.Member.Username</small>
                        <br />
                        <small>@last.CreateDate.ToString("yyyy-MM-dd HH:mm")</small>
                    }
                </td>
            </tr>
                        }
    </tbody>
</table>